package com.fhz.kotlinmvvm.contentp

import android.content.ContentProvider
import android.content.ContentValues
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.net.Uri
import androidx.room.Room
import androidx.sqlite.db.SupportSQLiteDatabase
import com.fhz.kotlinmvvm.dao.GoodsDao
import com.fhz.kotlinmvvm.utils.AppDatabase
import javax.inject.Inject

class MyContentProvider : ContentProvider() {


    lateinit var app:AppDatabase
    lateinit var  db: SupportSQLiteDatabase
    lateinit var  db1: SQLiteDatabase
    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int {
        TODO("Implement this to handle requests to delete one or more rows")
    }

    override fun getType(uri: Uri): String? {
        TODO(
            "Implement this to handle requests for the MIME type of the data" +
                    "at the given URI"
        )
    }

    override fun insert(uri: Uri, values: ContentValues?): Uri? {
//        db1.insert("goods",null,values)

        values?.let {
            db.insert("goods", SQLiteDatabase.CONFLICT_NONE, values)
        }
        return uri
    }

    override fun onCreate(): Boolean {
        app = context?.let {
            Room.databaseBuilder(
                it,
                AppDatabase::class.java, "a2102"
            ).build()
        }!!
        db = app.openHelper.writableDatabase
        return db != null
    }

    override fun query(
        uri: Uri, projection: Array<String>?, selection: String?,
        selectionArgs: Array<String>?, sortOrder: String?
    ): Cursor? {
        //select * from goods where id =  oderby
//        db1.query("goods",projection,selection,selectionArgs,null,null,sortOrder,null)
        //
        return db.query("select * from goods")
    }

    override fun update(
        uri: Uri, values: ContentValues?, selection: String?,
        selectionArgs: Array<String>?
    ): Int {
       return db1.update("goods",values,selection,selectionArgs)
    }
}